package la.iok.hzsvn.lewin.movie.cloud.service;

import la.iok.hzsvn.lewin.movie.cloud.entity.Role;
import la.iok.hzsvn.lewin.movie.cloud.mapper.RoleMapper;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.NotNullElement;
import la.iok.hzsvn.share.annotations.Nullable;
import la.iok.hzsvn.share.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role,RoleMapper,Long> implements RoleService{
    public RoleServiceImpl(RoleMapper roleMapper) {
        super(roleMapper);
    }

    @Override
    public Role select(String name) {
        if(StringUtils.isEmpty(name)){
            return null;
        }
        return mapper.selectByProperty("name",name);
    }

    @NotNull
    @Override
    public Long save(@NotNull Role entity) {
        mapper.insertSelective(entity);
        return entity.getId();
    }

    @NotNull
    @NotNullElement
    @Override
    public List<Role> list(@Nullable Collection<Long> ids) {
        if(ids == null){
            return Collections.emptyList();
        }
        ids = ids.stream().filter(Objects::nonNull).collect(Collectors.toSet());
        if(ids.size() == 0){
            return Collections.emptyList();
        }
        return mapper.listInProperty("id",ids);
    }
}
